package com.tuyrt.`as`.proj.main.fragment.home

import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import com.tuyrt.`as`.proj.common.ui.view.loadUrl
import com.tuyrt.`as`.proj.main.R
import com.tuyrt.`as`.proj.main.model.GoodsModel
import com.tuyrt.`as`.proj.main.model.selectPrice
import com.tuyrt.`as`.proj.main.route.HiRoute
import com.tuyrt.`as`.proj.main.route.HiRoute.Destination
import com.tuyrt.hi.library.klog.KLog
import com.tuyrt.hi.library.util.HiDisplayUtil
import com.tuyrt.hi.library.util.HiRes
import com.tuyrt.hi.ui.item.HiDataItem
import com.tuyrt.hi.ui.item.HiViewHolder
import kotlinx.android.synthetic.main.layout_home_goods_list_item1.*
import kotlinx.android.synthetic.main.layout_home_goods_list_item1.view.*

/**
 *
 */
open class GoodsItem(val goodsModel: GoodsModel, val hotTab: Boolean) : HiDataItem<GoodsModel, HiViewHolder>(goodsModel) {

    // 标签最大显示数目
    private val MAX_TAG_SIZE: Int = 4

    override fun onBindData(holder: HiViewHolder, position: Int) {
        val context = holder.itemView.context

        goodsModel.sliderImage?.let { holder.item_image.loadUrl(it) }
        holder.item_title.text = goodsModel.goodsName
        holder.item_price.text = selectPrice(goodsModel.groupPrice, goodsModel.marketPrice)
        holder.item_sale_desc.text = goodsModel.completedNumText

        // 标签
        val labelContainer = holder.itemView.item_label_container
        if (labelContainer != null) {
            if (!TextUtils.isEmpty(goodsModel.tags)) {
                labelContainer.visibility = View.VISIBLE
                val labels = goodsModel.tags!!.split(" ")
                for (index in labels.indices) {
                    val childCount = labelContainer.childCount

                    if (index > MAX_TAG_SIZE - 1) {
                        // 删除不用复用的多余 child : 倒叙删除
                        for (deleteIndex in childCount - 1 downTo MAX_TAG_SIZE - 1) {
                            labelContainer.removeViewAt(deleteIndex)
                        }
                        break
                    }
                    // 这里有个问题：labelView 复用
                    // 解决上下滑动复用的问题--重复创建的问题
                    val labelView = if (index > childCount - 1) {
                        val view = createLabelView(context, index != 0)
                        labelContainer.addView(view)
                        view
                    } else {
                        labelContainer.getChildAt(index) as TextView
                    }
                    labelView.text = labels[index]
                }
            } else {
                labelContainer.visibility = View.GONE
            }
        }

        if (!hotTab) { // 网格布局时，中间间隔实现：给左边 itemView设置右边距，右边itemView设置左边距
            val margin = HiDisplayUtil.dp2px(2f)
            val params = holder.itemView.layoutParams as RecyclerView.LayoutParams
            val parentLef = hiAdapter?.getAttachRecyclerView()?.left ?: 0
            val parentPaddingLeft = hiAdapter?.getAttachRecyclerView()?.paddingLeft ?: 0
            val itemLeft = holder.itemView.left
            if (itemLeft == (parentLef + parentPaddingLeft)) {
                params.rightMargin = margin
            } else {
                params.leftMargin = margin
            }
            holder.itemView.layoutParams = params
        }

        holder.itemView.setOnClickListener {
            KLog.d("GoodsItem#OnClick")
            val toJson = Gson().toJson(goodsModel)
            KLog.json(toJson)

            val bundle = Bundle()
            bundle.putString("goodsId", goodsModel.goodsId)
            bundle.putParcelable("goodsModel", goodsModel)
            HiRoute.startActivity(context, bundle, Destination.DETAIL_MAIN)
        }
    }

    override fun getItemLayoutRes(): Int {
        return if (hotTab) R.layout.layout_home_goods_list_item1 else R.layout.layout_home_goods_list_item2
    }

    override fun getSpanSize(): Int {
        return if (hotTab) super.getSpanSize() else 1
    }

    private fun createLabelView(context: Context, withLeftMargin: Boolean): TextView {
        val labelView = TextView(context)
        labelView.setTextColor(HiRes.getColor(R.color.color_e75))
        labelView.setBackgroundResource(R.drawable.shape_goods_label)
        labelView.textSize = 11f
        labelView.gravity = Gravity.CENTER
        val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, HiDisplayUtil.dp2px(16f))
        params.leftMargin = if (withLeftMargin) HiDisplayUtil.dp2px(4f) else 0
        labelView.layoutParams = params
        return labelView
    }
}